﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json.Serialization;
using System.Text.Json;

namespace ExchangeClient.DbCache.Model
{


    public class SapAssetScrapRequest
    {
        /// <summary>
        /// 财务资产编号（FinancialCode），
        /// </summary>
        public long FinancialCode { get; set; }

        /// <summary>
        /// 公司代码（CompanyCode），
        /// </summary>
        public string? CompanyCode { get; set; }

        /// <summary>
        /// 资产原值（
        /// </summary>
        [JsonConverter(typeof(DecimalToStringConverter))]
        public string? OriginalValue { get; set; }

        /// <summary>
        /// 累计折旧（LJZJ）
        /// </summary>
        [JsonConverter(typeof(DecimalToStringConverter))]
        public string? LJZJ { get; set; }

        /// <summary>
        /// 本金化日期
        /// </summary>
        public string? CapitalizationDate { get; set; }

        /// <summary>
        /// 报废日期
        /// </summary>
        public string? ScrapDate { get; set; }

        /// <summary>
        /// 成本中心
        /// </summary>
        public string? CenterCode { get; set; }

        /// <summary>
        /// 折旧开始日
        /// </summary>
        public string? ZJDate { get; set; }

        /// <summary>
        /// 使用年
        /// </summary>
        public string? UseYear { get; set; }

        /// <summary>
        /// 使用月
        /// </summary>
        public string? UseMonth { get; set; }

        /// <summary>
        /// 折旧码
        /// </summary>
        public string? ZJCode { get; set; }

        /// <summary>
        /// 资产系统编码
        /// </summary>
   
        public string? BarCode { get; set; }

        /// <summary>
        /// 资产收货
        /// </summary>
        public string? ZSFZCSHQX { get; set; }

        /// <summary>
        /// 是否推送完成
        /// </summary>
        public int ZTSWC { get; set; } = 0;

        /// <summary>
        /// 批次ID
        /// </summary>
        public string? BatchId { get; set; }

        /// <summary>
        /// 批次数据总的数量
        /// </summary>
        public string? BatchTotal { get; set; }
    


    }
    public class SapAssetScrapRequestView
    {
        /// <summary>
        /// 主键
        /// </summary>
        public Guid Id { get; set; } = new Guid();

        /// <summary>
        /// 财务资产编号（FinancialCode），
        /// </summary>
        public long FinancialCode { get; set; }

        /// <summary>
        /// 公司代码（CompanyCode），
        /// </summary>
        public string? CompanyCode { get; set; }

        /// <summary>
        /// 资产原值（
        /// </summary>
        [JsonConverter(typeof(DecimalToStringConverter))]
        public string? OriginalValue { get; set; }

        /// <summary>
        /// 累计折旧（LJZJ）
        /// </summary>
        [JsonConverter(typeof(DecimalToStringConverter))]
        public string? LJZJ { get; set; }

        /// <summary>
        /// 本金化日期
        /// </summary>
        public string? CapitalizationDate { get; set; }

        /// <summary>
        /// 报废日期
        /// </summary>
        public string? ScrapDate { get; set; }

        /// <summary>
        /// 成本中心
        /// </summary>
        public string? CenterCode { get; set; }

        /// <summary>
        /// 折旧开始日
        /// </summary>
        public string? ZJDate { get; set; }

        /// <summary>
        /// 使用年
        /// </summary>
        public string? UseYear { get; set; }

        /// <summary>
        /// 使用月
        /// </summary>
        public string? UseMonth { get; set; }

        /// <summary>
        /// 折旧码
        /// </summary>
        public string? ZJCode { get; set; }

        /// <summary>
        /// 资产系统编码
        /// </summary>

        public string? BarCode { get; set; }

        /// <summary>
        /// 资产收货
        /// </summary>
        public string? ZSFZCSHQX { get; set; }

        /// <summary>
        /// 是否推送完成
        /// </summary>
        public int ZTSWC { get; set; }

        /// <summary>
        /// 批次ID
        /// </summary>
        public string? BatchId { get; set; }

        /// <summary>
        /// 批次数据总的数量
        /// </summary>
        public string? BatchTotal { get; set; }
        /// <summary>
        /// 是否已完成 SAP 数据传入，true 表示已成功接收并处理传入流程
        /// </summary>
        public bool IsTransferred { get; set; } = false;

        /// <summary>
        /// 入库时间（CreateTime），记录数据入库时间，可用于数据追溯、排序等
        /// </summary>

        public string? CreateTime { get; set; } = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");


    }

    public class DecimalToStringConverter : JsonConverter<string>
    {
        public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            // 处理数值类型
            if (reader.TokenType == JsonTokenType.Number)
            {
                // 直接转为字符串，保留原始格式
                return reader.GetDecimal().ToString();
            }
            // 处理字符串类型
            else if (reader.TokenType == JsonTokenType.String)
            {
                return reader.GetString();
            }

            throw new JsonException($"无法将 {reader.TokenType} 转换为字符串");
        }

        public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
        {
            writer.WriteStringValue(value);
        }
    }
    /// <summary>
    /// 单个资产报废数据的处理结果（包含错误详情）
    /// </summary>
    public class ScrapAssetResult
    {
        // 返回状态码
        public int Code { get; set; }
        // 处理结果（成功/失败）
        public bool Success { get; set; }
        // 错误原因
        public string ErrorMessage { get; set; } = "";
        // 原始数据（方便定位是哪个item）
        public SapAssetScrapRequest Data { get; set; }
        
       
        
    }

}
